package com.example.coroutine

import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleNetworkScope
import com.example.coroutine.net.NetworkCoroutineScope
import com.example.coroutine.net.RedditApi
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val mainViewModel by viewModels<MainViewModel> {
        return@viewModels object : ViewModelProvider.NewInstanceFactory() {

            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return MainViewModel() as T
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val job = NetworkCoroutineScope.launch {
            val data = RedditApi.getInstance().getTop("androiddev", 10)
            Log.d("tag ", data.data.toString())
        }

        val job1 = lifecycleNetworkScope.launch {
            val data = RedditApi.getInstance().getTop("androiddev", 10)
            Log.d("tag ", data.data.toString())
            job.join()
        }


        mainViewModel.getData()

    }

}
